﻿using System;
using System.Web.UI;

namespace SisEagle.View.Web.Pedidos
{
    public partial class Selecionar_Produtos : System.Web.UI.Page
    {

        #region Singleton
        private SisEagleBO.CarrinhoComprasBO _CarrinhoComprasBusiness;
        private SisEagleBO.CarrinhoComprasBO CarrinhoComprasBusiness
        {
            get
            {
                if (_CarrinhoComprasBusiness == null)
                    _CarrinhoComprasBusiness = new SisEagleBO.CarrinhoComprasBO();

                return _CarrinhoComprasBusiness;
            }
        }

        private SisEagleBO.FuncionarioBO _FuncionarioBusiness;
        private SisEagleBO.FuncionarioBO FuncionarioBusiness
        {
            get
            {
                if (_FuncionarioBusiness == null)
                    _FuncionarioBusiness = new SisEagleBO.FuncionarioBO();

                return _FuncionarioBusiness;
            }
        }

        private SisEagleBO.ClientesBO _ClientesBusiness;
        private SisEagleBO.ClientesBO ClientesBusiness
        {
            get
            {
                if (_ClientesBusiness == null)
                    _ClientesBusiness = new SisEagleBO.ClientesBO();

                return _ClientesBusiness;
            }
        }

        private SisEagleBO.ControleVendasBO _ControleVendasBusiness;
        private SisEagleBO.ControleVendasBO ControleVendasBusiness
        {
            get
            {
                if (_ControleVendasBusiness == null)
                    _ControleVendasBusiness = new SisEagleBO.ControleVendasBO();

                return _ControleVendasBusiness;
            }
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {

            if(!(Request.QueryString["codCarrinho"] == null))
            {
            
                ////mostra o carrinho
                lblCodCarrinhoCompras.Text = Request.QueryString["codCarrinho"];
                
                SisEagleVO.CarrinhoComprasVO objCarrinhoComprasVO = new SisEagleVO.CarrinhoComprasVO();

                objCarrinhoComprasVO = CarrinhoComprasBusiness.PegaDadosCarrinho(Request.QueryString["codCarrinho"]);

                lblCodFuncionario.Text = objCarrinhoComprasVO.codFuncionario.ToString();
                lblNomeFuncionario.Text = FuncionarioBusiness.PegaNomeFuncionario(objCarrinhoComprasVO.codFuncionario.ToString());
                lblNomeCliente.Text = ClientesBusiness.PegaNomeCliente(objCarrinhoComprasVO.codCliente.ToString());
                
                CarregaGrid();

                //CalculaTotalCarrinho();
            
            }

        }

        protected void CarregaGrid()
        {
            gridProdutos.DataSource = ControleVendasBusiness.FindBy_CodCarrinhoCompras(Convert.ToInt16(lblCodCarrinhoCompras.Text));
            gridProdutos.DataBind();         

        }

        protected void btnCancelar_Click(object sender, ImageClickEventArgs e)
        {
            
            int reg = 0;
            SisEagleVO.CarrinhoComprasVO objCarrinhoComprasVO = new SisEagleVO.CarrinhoComprasVO();

            try
            {
                if (lblCodCarrinhoCompras.Text.Length > 0)
                {
                    objCarrinhoComprasVO.CodCarrinhoCompras = Convert.ToInt16(lblCodCarrinhoCompras.Text);
                    reg = CarrinhoComprasBusiness.Delete(objCarrinhoComprasVO);
                }
            }
            catch (Exception erro)
            {
                //depois usar o user control Notificaçao
                Response.Write("<script>alert('Erro ao cancelar o carrinho. " + erro.ToString() + "');</script>");
            }
            finally
            {
                //depois usar o user control Notificaçao
                if (reg > 0)
                {
                    Response.Write("<script>alert('Carrinho de Compras cancelado com sucesso.');</script>");
                    Response.Redirect("CarrinhoCompras.aspx");
                }
                else
                {
                    Response.Write("<script>alert('Erro ao cancelar o carinho de compras.');</script>");
                }
            }

        }

        protected void lkCatalogo_Click(object sender, EventArgs e)
        {
            Response.Redirect("Catalogo_Produtos.aspx?codCarrinho=" + lblCodCarrinhoCompras.Text);
        }

        protected void btnSalvar_Click(object sender, ImageClickEventArgs e)
        {
            //confirma carrinho
            string teste = null;
        }

        
    }
}